package org.pf.base.pfBlocks
{
	import Box2D.Common.Math.b2Vec2;
	import Box2D.Dynamics.b2Body;
	import Box2D.Dynamics.b2World;
	import flash.events.EventDispatcher;
	import flash.system.System;
	import org.pf.constructor.staff.B2DDebug;
	import org.pf.constructor.staff.PFBodyGraphics;
	import org.pf.interfaces.IPFCleaner;
	import org.pf.PFPrince;
	import org.pf.semaphore.events.PFStorageBodyTweensEvent;
	
	/**
	 * ...
	 * @author zeksa'
	 */
	public class PFCleaner extends EventDispatcher implements IPFCleaner
	{
		private var tmp_body:b2Body;
		
		public function PFCleaner():void
		{
		}
		
		public function clearBody(body:b2Body):void
		{
			body.GetUserData().toRemove = true;
		}
		
		public function clearPFBodyGraphics(body:b2Body):void
		{
			body.GetUserData().bodyGraphics.sprite.parent.removeChild(body.GetUserData().bodyGraphics.sprite);
			body.GetUserData().bodyGraphics.sprite = null;
		}
		
		public function clearBodyAndGraphics(body:b2Body):void
		{
			if (body)
			{
				if (body.GetUserData() && body.GetUserData().bodyGraphics.sprite.parent)
				{
					body.GetUserData().bodyGraphics.sprite.parent.removeChild(body.GetUserData().bodyGraphics.sprite);
					body.GetUserData().toRemove = true;
				}
				
			}
		}
		
		public function clearPFBodyGraphicsAfterTweener(graphics:PFBodyGraphics):void
		{
			var func:Function = function(e:PFStorageBodyTweensEvent):void
			{
				graphics.sprite.parent.removeChild(graphics.sprite);
				graphics.setSprite(null);
			}
			graphics.sprite.addEventListener(PFStorageBodyTweensEvent.BODY_ANIMATION_STOP, func);
		}
		
		public function clearEnviroment():void
		{
			if (PFPrince.getPrince().get_SystemCache().currentWorld)
			{
				if (PFPrince.getPrince().get_SystemCache().currentCache && PFPrince.getPrince().get_SystemCache().currentCache.ropes)
				{
					PFPrince.getPrince().get_SystemCache().currentCache.ropes = null;
					PFPrince.getPrince().get_SystemCache().currentCache.ropesNames = null;
				}
				
				for (var b:b2Body = PFPrince.getPrince().get_SystemCache().currentWorld.GetBodyList(); b; b = tmp_body)
				{
					tmp_body = b.GetNext();
					if (b.GetUserData() && b.GetUserData().bodyGraphics && b.GetUserData().bodyGraphics.sprite)
					{
						PFPrince.getPrince().get_SystemCache().layers.viewLayer.removeChild(b.GetUserData().bodyGraphics.sprite);
					}
					PFPrince.getPrince().get_SystemCache().currentWorld.DestroyBody(b);
				}
				tmp_body = null;
				PFPrince.getPrince().get_Updater().update();
			}
			clearLayers();
			PFPrince.getPrince().get_SystemCache().currentWorld = null;
			System.gc();
			PFPrince.getPrince().get_SystemCache().currentWorld = new b2World(new b2Vec2(0, 10), true);
			PFPrince.getPrince().get_SystemCache().currentWorld.SetDebugDraw(new B2DDebug(PFPrince.getPrince().get_SystemCache().layers.debugLayer));
		}
		
		public function clearLayers():void
		{
			PFPrince.getPrince().get_SystemCache().layers.clearLayers();
			PFPrince.getPrince().get_SystemCache().layers.revertPositions();
			PFPrince.getPrince().get_SystemCache().layers.revertSizes();
		}
	
	}

}